<?php
/**
 * cms后台新闻管理页
 * author wang
 */

define("PHP_IN",true);
require "../include/init.php";
require "checklogin.php";
require ROOT_PATH."cms/smart_connection.php";

$page = isset($_GET['page']) ? $_GET['page'] : 1 ;
$pagesize = 4;
$pagestart = ($page-1) * $pagesize ;

$sql = "SELECT count(*) AS num FROM news";
$result = $db->query($sql);
$row = $db->fetch_one($result);
$count = $row['num'];

$pagecount = ceil($count/$pagesize);
$page_links = cms_page_links($pagecount, $page, "news_list.php?$page");

$sql = "SELECT * FROM news ORDER BY newsid DESC LIMIT $pagestart,$pagesize ";
$result = $db->query($sql);
$news = $db->fetch_all($result);

if(isset($_GET['act']) &&  $_GET['act']=="del"){
    $deleteid = isset($_GET['deleteid']) ? $_GET['deleteid']:0;

    $sql = "DELETE FROM news WHERE newsid = $deleteid";
    $result = $db->query($sql);
    if($result){
        $sql = "SELECT count(*) AS num FROM news";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }

        header("Location:news_list.php?page=".$page);
    }else{
        alert("删除失败","news_list.php");
    }
}
if(isset($_GET['act']) && $_GET['act']=="delall" && !empty($_POST['idarr'])){
    $deleteid = isset($_POST['idarr']) ? implode(",",$_POST['idarr']) : "";

    $sql = "DELETE FROM news WHERE newsid IN ($deleteid)";
    $result = $db->query($sql);
    if($result){
        $sql = "SELECT count(*) AS num FROM news";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }
        header("Location:news_list.php?page=".$page);
    }else{
        alert("删除失败","news_list.php");
    }

}

$smarty->assign("news",$news);
$smarty->assign("page",$page);
$smarty->assign("pagecount",$pagecount);
$smarty->assign("pagesize",$pagesize);
$smarty->assign("page_links",$page_links);

$smarty->display("news_list.tpl");
$db->close();
?>
